/*
 * @Author: szx
 * @Date: 2022-06-21 14:57:01
 * @LastEditTime: 2022-06-21 15:08:14
 * @Description:
 * @FilePath: \leetcode\500-599\532\532.js
 */
/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
var findPairs = function (nums, k) {
    let res = 0;
    if (k == 0) {
        nums.sort((a, b) => a - b);
        let flag = true;
        for (let i = 0; i < nums.length - 1; i++) {
            if (nums[i] == nums[i + 1]) {
                if (flag) {
                    res++;
                    flag = false;
                }
                continue;
            }
            flag = true;
        }
        return res;
    }
    const set = new Set(nums);
    const arr = Array.from(set).sort((a, b) => a - b);
    for (let i of arr) {
        if (set.has(i + k)) res++;
    }
    return res;
};
